<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText"></topTitle>
    <div class="mainCon">
      <template v-if="roleHandle">
        <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
          <el-form :inline="true" :model="filterField" ref="searchConditions">
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="区域"
                v-show="roleHandle"
                class="row-padding-bottom"
              >
                <el-cascader
                  v-model="areaId"
                  :options="areasOptions"
                  @change="getAreaId"
                  clearable
                  filterable
                  placeholder="请选择"
                  :props="{ checkStrictly: true }"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="行业"
                v-show="roleHandle"
                class="row-padding-bottom"
              >
                <!--<el-select v-model="filterField.industryId" clearable filterable placeholder="请选择">
                <el-option v-for="item in industryIdOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>-->
                <el-cascader
                  v-model="industryId"
                  :options="industryOptionsNC"
                  @change="getIndustryId"
                  clearable
                  filterable
                  placeholder="请选择"
                  :props="{ checkStrictly: true }"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <template v-if="advanced">
              <el-col :lg="8" :xl="6">
                <el-form-item label="状态" class="row-padding-bottom">
                  <el-select
                    v-model="filterField.status"
                    clearable
                    filterable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in stateOptions"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="类型" class="row-padding-bottom">
                  <el-select
                    v-model="filterField.type"
                    clearable
                    filterable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in typeOptions"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="处罚情况" class="row-padding-bottom">
                  <el-select
                    v-model="filterField.punishCondition"
                    clearable
                    filterable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in isPunishOptions"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="预警时间">
                  <timeSelection
                    @getStartTimeValue="getStartTimeValue"
                    @getEndTimeValue="getEndTimeValue"
                  ></timeSelection>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item
                  label="处理时间"
                  class="row-padding-bottom"
                  v-show="roleHandle"
                >
                  <timeSelection
                    @getStartTimeValue="getStartTimeValueTwo"
                    @getEndTimeValue="getEndTimeValueTwo"
                    :startTime="startTimeValue"
                    :endTime="endTimeValue"
                  ></timeSelection>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item
                  label="触发企业"
                  v-show="roleHandle"
                  class="row-padding-bottom"
                >
                  <el-input
                    v-model="filterField.companyName"
                    placeholder="请输入企业名称"
                  />
                </el-form-item>
              </el-col>
            </template>
            <el-col :lg="8" :xl="6">
              <el-form-item label="">
                <a
                  @click="toggleAdvanced"
                  style="margin-left: 40px; color: #409eff; cursor: pointer"
                  class="btnColor"
                >
                  {{ advanced ? '收起' : '展开' }}
                  <i
                    :class="
                      advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                    "
                  ></i>
                </a>
                <el-button
                  type="primary"
                  icon="icon iconfont iconchazhao1"
                  @click="searchList"
                  class="searchBtn"
                >
                  <span>搜索</span>
                </el-button>
              </el-form-item>
              <el-form-item>
                <exportBtn
                  :dataLen="tableList.length"
                  :filterField="filterField"
                  :api="api"
                ></exportBtn>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </template>
      <template v-if="!roleHandle">
        <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
          <el-form :inline="true" :model="filterField" ref="searchConditions">
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="区域"
                v-show="roleHandle"
                class="row-padding-bottom"
              >
                <el-cascader
                  v-model="areaId"
                  :options="areasOptions"
                  @change="getAreaId"
                  clearable
                  filterable
                  placeholder="请选择"
                  :props="{ checkStrictly: true }"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="行业"
                v-show="roleHandle"
                class="row-padding-bottom"
              >
                <el-cascader
                  v-model="industryId"
                  :options="industryOptionsNC"
                  @change="getIndustryId"
                  clearable
                  filterable
                  placeholder="请选择"
                  :props="{ checkStrictly: true }"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.status"
                  clearable
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in stateOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="类型" class="row-padding-bottom">
                <el-select
                  v-model="filterField.type"
                  clearable
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in typeOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <template v-if="advanced">
              <el-col :lg="8" :xl="6">
                <el-form-item label="处罚情况" class="row-padding-bottom">
                  <el-select
                    v-model="filterField.punishCondition"
                    clearable
                    filterable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in isPunishOptions"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="预警时间">
                  <timeSelection
                    @getStartTimeValue="getStartTimeValue"
                    @getEndTimeValue="getEndTimeValue"
                  ></timeSelection>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item
                  label="处理时间"
                  class="row-padding-bottom"
                  v-show="roleHandle"
                >
                  <timeSelection
                    @getStartTimeValue="getStartTimeValueTwo"
                    @getEndTimeValue="getEndTimeValueTwo"
                    :startTime="startTimeValue"
                    :endTime="endTimeValue"
                  ></timeSelection>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item
                  label="触发企业"
                  v-show="roleHandle"
                  class="row-padding-bottom"
                >
                  <el-input
                    v-model="filterField.companyName"
                    placeholder="请输入企业名称"
                  />
                </el-form-item>
              </el-col>
            </template>
            <el-col :lg="8" :xl="6">
              <el-form-item label="">
                <a
                  @click="toggleAdvanced"
                  style="margin-left: 40px; color: #409eff; cursor: pointer"
                  class="btnColor"
                >
                  {{ advanced ? '收起' : '展开' }}
                  <i
                    :class="
                      advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                    "
                  ></i>
                </a>
                <el-button
                  type="primary"
                  icon="icon iconfont iconchazhao1"
                  @click="searchList"
                  class="searchBtn"
                >
                  <span>搜索</span>
                </el-button>
              </el-form-item>
              <el-form-item>
                <exportBtn
                  :dataLen="tableList.length"
                  :filterField="filterField"
                  :api="api"
                ></exportBtn>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </template>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          @sort-change="sortChange"
          :max-height="tableHeight"
          :data="tableList"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          v-loading="loading"
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="description"
            label="预警类型"
            :formatter="formatTd"
            min-width="150"
          ></el-table-column>
          <el-table-column prop="reason" label="预警原因" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.reason"
                :columnName="'reason'"
              ></copyText>
            </template>
          </el-table-column>
          <template v-if="roleHandle">
            <el-table-column prop="companyName" label="触发企业" width="230">
              <template slot-scope="scope">
                <copyText
                  :tableIndex="scope.$index"
                  :copyTextName="scope.row.companyName"
                  :columnName="'companyName'"
                ></copyText>
              </template>
            </el-table-column>
          </template>
          <el-table-column
            prop="alertCreateTime"
            label="预警时间"
            sortable="custom"
            align="center"
            min-width="120"
          >
            <template slot-scope="scope">
              {{
                scope.row.alertCreateTime != null
                  ? scope.row.alertCreateTime.substr(0, 10)
                  : '--'
              }}
            </template>
          </el-table-column>
          <el-table-column
            prop="alertDayCount"
            label="预警时长"
            sortable="custom"
            align="center"
            :formatter="formatTd"
            min-width="120"
          ></el-table-column>
          <el-table-column
            prop="handleCondition"
            label="处罚情况"
            align="center"
            width="120"
            :formatter="formatTd"
          ></el-table-column>
          <!-- <el-table-column prop="handleCondition" label="处罚情况" width='230'>
                    <template slot-scope="scope">
                        <copyText :tableIndex='scope.$index' :copyTextName='scope.row.handleCondition' :columnName="'handleCondition'"></copyText>
                    </template>
                </el-table-column> -->
          <el-table-column
            prop="handleCreateTime"
            label="处理时间"
            sortable="custom"
            align="center"
            min-width="120"
          >
            <template slot-scope="scope">
              {{
                scope.row.handleCreateTime != null
                  ? scope.row.handleCreateTime.substr(0, 10)
                  : '--'
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="left"
            fixed="right"
            width="110px"
          >
            <template slot-scope="scope">
              <el-link
                v-if="scope.row.status === '已处理'"
                type="primary"
                v-show="isShowDetail"
                @click="handleAlert(scope.$index, scope.row)"
              >
                查看详情
              </el-link>

              <div v-if="scope.row.status != '已处理'">
                <el-link
                  type="primary"
                  @click="handleAlert(scope.$index, scope.row)"
                  v-if="hasHandleBtn"
                >
                  处理
                </el-link>
                <span v-else>--</span>
              </div>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
  </div>
</template>
<script>
import scrollTable from '@/mixin/scrollTable.js'
import { mapActions, mapState } from 'vuex'

export default {
  name: 'alertProjectList',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '预警列表',
      advanced: false,
      roleHandle: false, //标识监察还是企业用户登录，false为企业
      //查询条件数据封装
      stateOptions: [],
      typeOptions: [],
      isPunishOptions: [],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
      },
      //表格数据
      tableList: [],
      requestUrl: '',
      isShow: false, //列表加载
      loading: true,
      dateValue: [],
      handelDate: [],
      //变量，联动数据
      areaId: '',
      industryId: '',
      //搜索条件
      filterField: {
        status: '',
        type: '',
        punishCondition: '',
        alertStartTime: '',
        alertEndTime: '',
        handleStartTime: '',
        handleEndTime: '',
        areaId: '',
        industryId: '',
        companyName: '',
        loading: true,
        isShow: false,
      },
      hasHandleBtn: false,
      loginType: 'QY', //登录类型
      isShowDetail: false,
      api: '',
    }
  },
  computed: { ...mapState(['areasOptions', 'industryOptionsNC']) },

  created() {
    this.$store.state.activeMenu = '/home/project/alertProjectList'
    this.loginType = localStorage.getItem('loginType')

    //从企业首页跳转进入传参 By JY
    this.filterField.type = this.$route.query.type
    if (this.$route.query.state === 'nodeal') {
      this.filterField.status = '01'
    }
    //依据登录页逻辑判断登录用户类型
    if (localStorage.length > 0) {
      //判断监察还是企业用户登录
      // var cks = document.cookie.split('; ');
      // var role = cks[0].split("_");
      // var rs = role[1].split("=");
      if (
        localStorage.getItem('loginType') === 'QY' ||
        localStorage.getItem('loginType') === 'XM'
      ) {
        this.roleHandle = false
        this.requestUrl = 'api-a/nc/alert/expand/listbypage'
        this.api = 'api-s/report/export_QY_NC_ALERT'
      } else {
        this.roleHandle = true
        this.requestUrl = 'api-a/nc/alert/expand/monitor/listbypage'
        this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
        this.getIndustryNC({})
        // 监察端首页传过来的未处理参数值
        this.filterField.status = this.$route.query.notHandle
        this.api = 'api-s/report/export_JC_NC_ALERT'
      }
    } else {
      this.roleHandle = true
      this.requestUrl = 'api-a/nc/alert/expand/monitor/listbypage'
      // 监察端首页传过来的未处理参数值
      this.filterField.status = this.$route.query.notHandle
      this.api = 'api-s/report/export_JC_NC_ALERT'
    }
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    //初始化选择列表
    this.getStateData('api-bs/base/dicitem/findbytype', { type: 'ALERT_STATE' }) //状态下拉框
    this.getTypeData('api-bs/base/dicitem/findbytype', { type: 'ALERT_TYPE' }) //类别下拉框
    this.getPunishData('api-bs/base/dicitem/findbytype', {
      type: 'ALEAR_SOLUTION',
    }) //处罚情况下拉框

    //处理按钮权限 (企业、监察)
    if (this.loginType === 'QY') {
      this.hasHandleBtn =
        this.$store.state.buttonLimitsArr.indexOf('ncAlertList:update') > -1
    } else {
      this.hasHandleBtn =
        this.$store.state.buttonLimitsArr.indexOf('ncAlertList:update') > -1 ||
        this.$store.state.buttonLimitsArr.indexOf('ncAlertList:delete') > -1
    }
    this.isShowDetail =
      this.$store.state.buttonLimitsArr.indexOf('ncAlertList:detail') > -1
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    ...mapActions(['getAreas', 'getIndustryNC']),
    /**
     * @description: 获取开始日期
     * @param {*} urls
     * @param {*} datas
     */
    getStartTimeValue(val) {
      if (val) {
        this.filterField.alertStartTime = val
      } else {
        this.filterField.alertStartTime = ''
      }
    },
    /**
     * @description: 获取结束日期
     * @param {*} urls
     * @param {*} datas
     */
    getEndTimeValue(val) {
      if (val) {
        this.filterField.alertEndTime = val
      } else {
        this.filterField.alertEndTime = ''
      }
    },
    /**
     * @description: 获取开始日期
     * @param {*} urls
     * @param {*} datas
     */
    getStartTimeValueTwo(val) {
      if (val) {
        this.filterField.handleStartTime = val
      } else {
        this.filterField.handleStartTime = ''
      }
    },
    /**
     * @description: 获取结束日期
     * @param {*} urls
     * @param {*} datas
     */
    getEndTimeValueTwo(val) {
      if (val) {
        this.filterField.handleEndTime = val
      } else {
        this.filterField.handleEndTime = ''
      }
    },
    getStateData(urls, datas) {
      this.axios({
        method: 'post',
        url: urls,
        data: datas,
      })
        .then((resp) => {
          this.stateOptions = resp.data.result
        })
        .catch((resp) => {})
    },
    getTypeData(urls, datas) {
      this.axios({
        method: 'post',
        url: urls,
        data: datas,
      })
        .then((resp) => {
          this.typeOptions = resp.data.result
        })
        .catch((resp) => {})
    },
    getPunishData(urls, datas) {
      this.axios({
        method: 'post',
        url: urls,
        data: datas,
      })
        .then((resp) => {
          this.isPunishOptions = resp.data.result
        })
        .catch((resp) => {})
    },
    handleAlert(index, row) {
      //boolean在传到子页面时，刷新子页面会转成字符串。顾需将boolean转成字符串表示
      this.$router.push({
        path: '/home/project/alertDetail',
        name: 'alertDetail',
        query: {
          alertId: row.id,
          doEvent: row.status,
          isShow: this.roleHandle == true ? 'yes' : 'no',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/project/alertProjectList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    //获取选中的区域最后一级id
    getAreaId() {
      // if(this.filterField.areaId.length>0){
      //   var areaId = this.filterField.areaId[this.filterField.areaId.length - 1];
      //   this.filterField.areaId = areaId;
      // }
      var areaId = this.areaId[this.areaId.length - 1]

      this.filterField.areaId = areaId
    },
    //获取选中的行业最后一级id
    getIndustryId() {
      // if(this.filterField.industryId.length>0){
      //   var industryId = this.filterField.industryId[this.filterField.industryId.length - 1];
      //   this.filterField.industryId = industryId;
      // }
      var industryId = this.industryId[this.industryId.length - 1]

      this.filterField.industryId = industryId
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
  },
}
</script>

<style scoped lang="less">
.mainCon {
  top: 198px;
}
@import '../../../static/css/filterArea.less';
</style>
